using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CChange : CBaseDynamic, IComparable<CChange>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CChange(CChange original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _changeNodeGuid = original.ChangeNodeGuid;
            _changeTypeId = original.ChangeTypeId;
            _changeData = original.ChangeData;
            _changeDataIsGz = original.ChangeDataIsGz;
            _changeDetected = original.ChangeDetected;
        }

        //Protected (Datareader/Dataset)
        protected CChange(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CChange(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _changeId = long.MinValue;
            _changeNodeGuid = Guid.Empty;
            _changeTypeId = int.MinValue;
            _changeData = new byte[]{};
            _changeDataIsGz = false;
            _changeDetected = DateTime.MinValue;
        }
        #endregion

        #region Members
        protected long _changeId;
        protected Guid _changeNodeGuid;
        protected int _changeTypeId;
        protected byte[] _changeData;
        protected bool _changeDataIsGz;
        protected DateTime _changeDetected;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public long ChangeId    {   get { return _changeId;   } }

        //Table Columns (Read/Write)
        public Guid ChangeNodeGuid    {   get { return _changeNodeGuid; } set { _changeNodeGuid = value; }    }
        public int ChangeTypeId    {   get { return _changeTypeId; } set { _changeTypeId = value; }    }
        public byte[] ChangeData    {   get { return _changeData; } set { _changeData = value; }    }
        public bool ChangeDataIsGz    {   get { return _changeDataIsGz; } set { _changeDataIsGz = value; }    }
        public DateTime ChangeDetected    {   get { return _changeDetected; } set { _changeDetected = value; }    }

        //View Columns (ReadOnly)

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "Changes";
        public const string VIEW_NAME       = "";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "ChangeId DESC";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CChange other) {   return this.ChangeId.CompareTo(other.ChangeId) *-1;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "ChangeId";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _changeId; }
              set { _changeId = Convert.ToInt64(value); }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CChange(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CChange(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CChangeList();               }
        protected override IList MakeList(int capacity)     {   return new CChangeList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _changeId = CAdoData.GetLong(dr, "ChangeId");
            _changeNodeGuid = CAdoData.GetGuid(dr, "ChangeNodeGuid");
            _changeTypeId = CAdoData.GetInt(dr, "ChangeTypeId");
            _changeData = CAdoData.GetBytes(dr, "ChangeData");
            _changeDataIsGz = CAdoData.GetBool(dr, "ChangeDataIsGz");
            _changeDetected = CAdoData.GetDate(dr, "ChangeDetected");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _changeId = CAdoData.GetLong(dr, "ChangeId");
            _changeNodeGuid = CAdoData.GetGuid(dr, "ChangeNodeGuid");
            _changeTypeId = CAdoData.GetInt(dr, "ChangeTypeId");
            _changeData = CAdoData.GetBytes(dr, "ChangeData");
            _changeDataIsGz = CAdoData.GetBool(dr, "ChangeDataIsGz");
            _changeDetected = CAdoData.GetDate(dr, "ChangeDetected");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("ChangeId", CAdoData.NullVal(_changeId));
            data.Add("ChangeNodeGuid", CAdoData.NullVal(_changeNodeGuid));
            data.Add("ChangeTypeId", CAdoData.NullVal(_changeTypeId));
            data.Add("ChangeData", CAdoData.NullVal(_changeData));
            data.Add("ChangeDataIsGz", CAdoData.NullVal(_changeDataIsGz));
            data.Add("ChangeDetected", CAdoData.NullVal(_changeDetected));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CChangeList SelectAll()                                                          {    return (CChangeList)base.SelectAll();                              }
        public new CChangeList SelectAll(string orderBy)                                            {    return (CChangeList)base.SelectAll(orderBy);                       }
        public new CChangeList SelectWhere(string colName, ESign sign, object colValue)             {    return (CChangeList)base.SelectWhere(colName, sign, colValue);     }
        public new CChangeList SelectWhere(CCriteria where)                                         {    return (CChangeList)base.SelectWhere(where);                       }
        public new CChangeList SelectWhere(CCriteriaList where)                                     {    return (CChangeList)base.SelectWhere(where);                       }
        public new CChangeList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CChangeList)base.SelectWhere(where, tableOrJoin);          }
        public new CChangeList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CChangeList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CChangeList SelectWhere(string unsafeWhereClause)                                {    return (CChangeList)base.SelectWhere(unsafeWhereClause);           }
        public     CChangeList SelectById(long changeId)                              {    return (CChangeList)base.SelectById(changeId);       }
        public     CChangeList SelectByIds(List<long> ids)                        {    return (CChangeList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CChangeList SelectAll(  CPagingInfo pi)                                              {    return (CChangeList)base.SelectAll(  pi);                              }
        public new CChangeList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CChangeList)base.SelectWhere(pi, name, sign, value);           }
        public new CChangeList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CChangeList)base.SelectWhere(pi, criteria);                    }
        public new CChangeList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CChangeList)base.SelectWhere(pi, criteria);                    }
        public new CChangeList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CChangeList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CChangeList SelectByIds(CPagingInfo pi, List<long> ids)            {    return (CChangeList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CChangeList SelectAll(                                                                                           IDbTransaction tx)  {    return (CChangeList)base.SelectAll(tx);                                                     }
        public new CChangeList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CChangeList)base.SelectAll(orderBy,                                         tx);    }
        public new CChangeList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CChangeList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CChangeList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CChangeList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CChangeList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CChangeList)base.SelectWhere(               criteria,                       tx);    }
        public new CChangeList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CChangeList)base.SelectWhere(               criteria,                       tx);    }
        public new CChangeList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CChangeList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CChangeList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CChangeList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CChangeList SelectById(long changeId,                                                              IDbTransaction tx)  {    return (CChangeList)base.SelectById(changeId,                         tx);    }
        public     CChangeList SelectByIds(List<long> ids,                                                        IDbTransaction tx)  {    return (CChangeList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CChangeList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CChangeList)base.MakeList(storedProcName,             txOrNull);  }
        public new CChangeList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CChangeList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CChangeList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CChangeList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CChangeList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CChangeList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CChangeList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CChangeList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CChangeList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CChangeList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CChangeList MakeList(DataSet              ds) { return (CChangeList)base.MakeList(ds);        }
        protected new CChangeList MakeList(DataTable            dt) { return (CChangeList)base.MakeList(dt);        }
        protected new CChangeList MakeList(DataRowCollection  rows) { return (CChangeList)base.MakeList(rows);      }
        protected new CChangeList MakeList(IDataReader          dr) { return (CChangeList)base.MakeList(dr);        }
        protected new CChangeList MakeList(object           drOrDs) { return (CChangeList)base.MakeList(drOrDs);    }        
        protected new CChangeList MakeList(byte[]             gzip) { return (CChangeList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CChangeList SelectByNodeGuid(Guid changeNodeGuid)    {    return SelectWhere(new CCriteriaList("ChangeNodeGuid", changeNodeGuid));    }
        public CChangeList SelectByTypeId(int changeTypeId)    {    return SelectWhere(new CCriteriaList("ChangeTypeId", changeTypeId));    }
        public CChangeList SelectByDataIsGz(bool changeDataIsGz)    {    return SelectWhere(new CCriteriaList("ChangeDataIsGz", changeDataIsGz));    }

        //Paged
        public CChangeList SelectByNodeGuid(CPagingInfo pi, Guid changeNodeGuid)    {    return SelectWhere(pi, new CCriteriaList("ChangeNodeGuid", changeNodeGuid));    }
        public CChangeList SelectByTypeId(CPagingInfo pi, int changeTypeId)    {    return SelectWhere(pi, new CCriteriaList("ChangeTypeId", changeTypeId));    }
        public CChangeList SelectByDataIsGz(CPagingInfo pi, bool changeDataIsGz)    {    return SelectWhere(pi, new CCriteriaList("ChangeDataIsGz", changeDataIsGz));    }

        //Count
        public int SelectCountByNodeGuid(Guid changeNodeGuid)   {   return SelectCount(new CCriteriaList("ChangeNodeGuid", changeNodeGuid));     }
        public int SelectCountByTypeId(int changeTypeId)   {   return SelectCount(new CCriteriaList("ChangeTypeId", changeTypeId));     }
        public int SelectCountByDataIsGz(bool changeDataIsGz)   {   return SelectCount(new CCriteriaList("ChangeDataIsGz", changeDataIsGz));     }

        //Transactional
        public CChangeList SelectByNodeGuid(Guid changeNodeGuid, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ChangeNodeGuid", changeNodeGuid), tx);    }
        public CChangeList SelectByTypeId(int changeTypeId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ChangeTypeId", changeTypeId), tx);    }
        public CChangeList SelectByDataIsGz(bool changeDataIsGz, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ChangeDataIsGz", changeDataIsGz), tx);    }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "ChangeId", this.ChangeId);
            Store(w, "ChangeNodeGuid", this.ChangeNodeGuid);
            Store(w, "ChangeTypeId", this.ChangeTypeId);
            Store(w, "ChangeData", this.ChangeData);
            Store(w, "ChangeDataIsGz", this.ChangeDataIsGz);
            Store(w, "ChangeDetected", this.ChangeDetected);
        }
        #endregion

    }
}
